package me.wangshier.leetcode.easy;

/**
 * 4. 数字分组求偶数和
 * @Author: tongqianwen
 * @Date: 2025/3/2
 */
public class GroupSumEven {
    public static int solution(int[] numbers) {
        // 初始化二维数组来存储每个数字组的奇数和偶数数量
        int[][] counts = new int[numbers.length][2];

        // 统计每个数字组的奇数和偶数数量
        for (int i = 0; i < numbers.length; i++) {
            String numStr = String.valueOf(numbers[i]);
            for (char c : numStr.toCharArray()) {
                int digit = c - '0';
                if (digit % 2 == 0) {
                    counts[i][1]++; // 偶数
                } else {
                    counts[i][0]++; // 奇数
                }
            }
        }

        int[][] sums = new int[numbers.length][2];
        for (int i = 0; i < numbers.length; i++) {
            int oddNum = counts[i][0]; // 奇数数量
            int evenNum = counts[i][1];// 偶数数量

            if(i==0) {
                sums[i][0] = oddNum;   // 奇数组合数
                sums[i][1] = evenNum;   // 偶数组合数
            }else {
                // 奇数组合数
                sums[i][0] = sums[i-1][0] * evenNum + sums[i-1][1] * oddNum;
                // 偶数组合数
                sums[i][1] = sums[i-1][1] * evenNum + sums[i-1][0] * oddNum;
            }
        }

        return sums[numbers.length-1][1];
    }


    public static void main(String[] args) {
        // You can add more test cases here
        System.out.println(solution(new int[]{123, 456, 789}) == 14);
        System.out.println(solution(new int[]{123456789}) == 4);
        System.out.println(solution(new int[]{14329, 7568}) == 10);
    }

}